import requests
import xlrd
import time
import smtplib

dic={}

def excel(i):
    global user
    global pwd
    workBook = xlrd.open_workbook('data/all.xls')
    sheet1_content1 = workBook.sheet_by_index(0);
    id=sheet1_content1.cell(i, 0).value
    print(id)
    return id

def sprider1():
    head1 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'
    }
    url='http://jwxtapp.nmu.edu.cn:8080/bfmzdxhd/patriarch/login?userNo='+user+'&pwd='+pwd
    r1=requests.get(url,headers=head1)
    r1.encoding='utf-8'
    json_token=r1.json()
    token=json_token['data']['token']
    # print(token)
    return token

def sprider2(token):
    head2 = {
    'Referer': 'http://jwxtapp.nmu.edu.cn:8080/jzdxsjd/',
    'token': token,
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'
    }

    r2 = requests.get('http://jwxtapp.nmu.edu.cn:8080/bfmzdxhd/student/termGPA?semester=&type=', headers=head2)
    r2.encoding='utf-8'
    temp=r2.json()
    grade=temp["data"][0]
    return grade
    # print(grade)

    # print(r2.text)

def extract(grade):
    global dic
    i=0
    courseName=[]
    credit = []
    score = []
    total=[]
    studentName=grade["name"]
    x=len(grade['achievement'])
    while i<x:
        courseName.append(grade['achievement'][i]['courseName'])
        credit.append(grade['achievement'][i]['credit'])
        score.append(grade['achievement'][i]['fraction'])

        total.append(grade['achievement'][i]['courseName'])
        total.append(grade['achievement'][i]['credit'])
        total.append(grade['achievement'][i]['fraction'])
        i=i+1
    dic[studentName]=total


i=0
while i<1268:  # 84
    j = 1
    try:
        id=excel(i)
        user = id
        pwd = id[-6:]
        token = sprider1()
        grade = sprider2(token)
        extract(grade)
        i = i + 1
    except Exception:
        j=j+1
        i = i + 1

print(dic)

